var log = require('../utils/log');
var netbus = require('../netbus/netbus');
var proto_man = require('../netbus/proto_man');


//json 编码/解码
var data = {
    uname: 'xiaoming',
    upwd: '123456',
}

var buf = proto_man.encode_cmd(netbus.PROTO_JSON,1,1,data);
log.info('JSON数据编码：',buf);
log.error('json length:', buf.length);
var cmd = proto_man.decode_cmd(netbus.PROTO_JSON,buf);
log.info('JSON数据解码：',cmd);
// end 


//二进制
function encode_cmd_1_1(body){
    console.log('======encode_cmd_1_1=========',body)
    let stype = 1;
    let ctype = 1;

    var total_len = 2 + 2 + body.uname.length + 2 + 2 + body.upwd.length;
    var buf = Buffer.allocUnsafe(total_len);
    buf.writeUInt16LE(stype,0);
    buf.writeUInt16LE(ctype,2);

    buf.writeUInt16LE(body.uname.length, 4); // 4, 5
	buf.write(body.uname, 6); // 6写入uname的字符串	

    var offset = 6 + body.uname.length; 
    buf.writeUInt16LE(body.upwd.length,offset);
    buf.write(body.upwd, offset+2);
    console.log('======encode_cmd_1_1===2======',buf)
    return buf;
}


function decode_cmd_1_1(cmd_buf) {
	var stype = 1;
	var ctype = 1;
	// uname
	var uname_len = cmd_buf.readUInt16LE(4);
	if((uname_len + 2 + 2 + 2) > cmd_buf.length) {
		return null;
	}
	
	var uname = cmd_buf.toString("utf8", 6, 6 + uname_len);
	if (!uname) {
		return null;
	}
	// end 

	var offset = 6 + uname_len;
	var upwd_len = cmd_buf.readUInt16LE(offset);
	if ((offset + upwd_len + 2) > cmd_buf.length) {
		return null;
	}

	var upwd = cmd_buf.toString("utf8", offset + 2, offset + 2 + upwd_len);
	var cmd = {
		0: 1,
		1: 1,
		2: {
			"uname": uname,
			"upwd":  upwd,
		}
	};
	return cmd;	
}
proto_man.reg_encoder(1, 1, encode_cmd_1_1);
proto_man.reg_decoder(1, 1, decode_cmd_1_1);
//end

var proto_cmd_buf = proto_man.encode_cmd(netbus.PROTO_BUF, 1, 1, data);
log.info('编码二进制数据：', proto_cmd_buf);
log.error(proto_cmd_buf.length);
cmd = proto_man.decode_cmd(netbus.PROTO_BUF, proto_cmd_buf);
log.info('解码二进制数据：', cmd);